﻿using System;
using System.Collections.Generic;

using System.Text;
using TimKen.DataEntity;
using System.Data.SqlClient;
using System.Data;

namespace TimKen.DataAccess
{
  /// <summary>
	/// 数据访问类Job。
	/// </summary>
    public class JobDAO
    {
        public JobDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("JobID", "Job");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int JobID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@JobID", SqlDbType.BigInt)};
            parameters[0].Value = JobID;

            int result = DbHelperSQL.RunProcedure("UP_Job_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(Job model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@JobID", SqlDbType.BigInt,8),
					new SqlParameter("@JobNo", SqlDbType.NVarChar,50),
					new SqlParameter("@JobName", SqlDbType.NVarChar,100),
					new SqlParameter("@ValidFrom", SqlDbType.DateTime),
					new SqlParameter("@ValidTo", SqlDbType.DateTime),
					new SqlParameter("@JobDesc", SqlDbType.NVarChar,2000),
					new SqlParameter("@Qualifications", SqlDbType.NVarChar,1000),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = model.JobID;
            parameters[1].Value = model.JobNo;
            parameters[2].Value = model.JobName;
            parameters[3].Value = model.ValidFrom;
            parameters[4].Value = model.ValidTo;
            parameters[5].Value = model.JobDesc;
            parameters[6].Value = model.Qualifications;
            parameters[7].Value = model.CreateDate;
            parameters[8].Value = model.LanguageID;

            DbHelperSQL.RunProcedure("UP_Job_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(Job model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@JobID", SqlDbType.BigInt,8),
					new SqlParameter("@JobNo", SqlDbType.NVarChar,50),
					new SqlParameter("@JobName", SqlDbType.NVarChar,100),
					new SqlParameter("@ValidFrom", SqlDbType.DateTime),
					new SqlParameter("@ValidTo", SqlDbType.DateTime),
					new SqlParameter("@JobDesc", SqlDbType.NVarChar,2000),
					new SqlParameter("@Qualifications", SqlDbType.NVarChar,1000),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@LanguageID", SqlDbType.Int,4)};
            parameters[0].Value = model.JobID;
            parameters[1].Value = model.JobNo;
            parameters[2].Value = model.JobName;
            parameters[3].Value = model.ValidFrom;
            parameters[4].Value = model.ValidTo;
            parameters[5].Value = model.JobDesc;
            parameters[6].Value = model.Qualifications;
            parameters[7].Value = model.CreateDate;
            parameters[8].Value = model.LanguageID;

            DbHelperSQL.RunProcedure("UP_Job_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int JobID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@JobID", SqlDbType.BigInt)};
            parameters[0].Value = JobID;

            DbHelperSQL.RunProcedure("UP_Job_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public  Job GetModel(int JobID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@JobID", SqlDbType.BigInt)};
            parameters[0].Value = JobID;

            Job model = new Job();
            DataSet ds = DbHelperSQL.RunProcedure("UP_Job_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["JobID"].ToString() != "")
                {
                    model.JobID = int.Parse(ds.Tables[0].Rows[0]["JobID"].ToString());
                }
                model.JobNo = ds.Tables[0].Rows[0]["JobNo"].ToString();
                model.JobName = ds.Tables[0].Rows[0]["JobName"].ToString();
                if (ds.Tables[0].Rows[0]["ValidFrom"].ToString() != "")
                {
                    model.ValidFrom = DateTime.Parse(ds.Tables[0].Rows[0]["ValidFrom"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidTo"].ToString() != "")
                {
                    model.ValidTo = DateTime.Parse(ds.Tables[0].Rows[0]["ValidTo"].ToString());
                }
                model.JobDesc = ds.Tables[0].Rows[0]["JobDesc"].ToString();
                model.Qualifications = ds.Tables[0].Rows[0]["Qualifications"].ToString();
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LanguageID"].ToString() != "")
                {
                    model.LanguageID = int.Parse(ds.Tables[0].Rows[0]["LanguageID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select JobID,JobNo,JobName,ValidFrom,ValidTo,JobDesc,Qualifications,CreateDate,LanguageID ");
            strSql.Append(" FROM Job ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex,int LanguageID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
				    new SqlParameter("@LanguageID", SqlDbType.Int),
					};
            parameters[0].Value = PageSize;
            parameters[1].Value = PageIndex;
            parameters[2].Value = LanguageID;

            return DbHelperSQL.RunProcedure("UP_Job_GetPageList", parameters, "ds");
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
            parameters[0].Value = "Job";
            parameters[1].Value = "JobID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 1;
            parameters[5].Value = 1;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
        }



        #endregion  成员方法
    }
}
